<?php
class Problem {
	
public function __construct( &$alt_sql = false){
		global $sql;
		if ($alt_sql==false){
			$this->sql=$sql;
		} else {
			$this->sql=&$alt_sql;
		}	
	}
	

	/**
	 * Adds a new Problem to the database.
	 *
	 * @return boolean
	 */
	public static function insert($sql, $vastus, $vastus2, $vastus3, $argumendid , $kirjeldus, $punkte, $kontrolltoo ) {
		//global $sql;
		$query = "INSERT INTO ulesanne ( idulesanne, tekst, argumendid, vastus, vastus2, vastus3, maks_punkte, kontrolltoo) 
			VALUES ( NULL, '".mysql_escape_string($kirjeldus)."',
			 '".mysql_escape_string($argumendid)."',
			  '".mysql_escape_string($vastus)."',
			  '".mysql_escape_string($vastus2)."',
			  '".mysql_escape_string($vastus3)."',
			   '".$punkte."',
			    '".$kontrolltoo."' )";
		
		$sql->query($query);
		if( $sql->affectedRows() == 1 ) {
			return true;
		}
		
		return false;
	}
	/**
	 * For testing purposes
	 */
	public static function testProblem($sql, $vastus, $argumendid , $kirjeldus) {
		//global $sql;
		$query="UPDATE ulesanne u SET u.vastus = '".mysql_escape_string($vastus)."', u.argumendid ='".mysql_escape_string($argumendid)."',
		u.tekst ='".mysql_escape_string($kirjeldus)."' 
			WHERE u.idulesanne = '1' LIMIT 1";
		$sql->query($query);
		
		if( !($sql->affectedRows() == 1) ) {
			//check if maybe the data that is being set is already set in the db 
			$query = "SELECT * FROM ulesanne u WHERE u.vastus = '".$vastus."'
			 AND u.argumendid ='".mysql_escape_string($argumendid)."' AND 
			u.tekst ='".mysql_escape_string($kirjeldus)."' 
			AND u.idulesanne = '1' LIMIT 1";
			$sql->query($query);
			if(count($sql->selectQuery($query))==1){
					return true;
					} else return false;
		} else return true;
	}
	public static function problemAnswer($sql, $questionId) {
		//global $sql;
		$query="SELECT u.vastus, u.vastus2, u.vastus3, u.argumendid FROM ulesanne u WHERE u.idulesanne = '".$questionId."' LIMIT 1";
		$result=$sql->selectQuery($query);
		if(sizeOf($result)>0){
			return $result;
		}else return false;
	}
	
	public static function testProblemDescription($sql) {
		//global $sql;
		$query="SELECT u.argumendid, u.tekst, u.vastus FROM ulesanne u WHERE u.idulesanne = '1' LIMIT 1";
		$result=$sql->selectQuery($query);
		if(sizeOf($result)>0){
			return $result;
		}else return false;
	}
	
	//return all the questions for a test
	public static function problemDescription($sql, $testId) {
		//global $sql;
		$query="SELECT u.idulesanne, u.argumendid, u.tekst FROM ulesanne u WHERE u.kontrolltoo = '".$testId."'";
		$result=$sql->selectQuery($query);
		if(sizeOf($result)>0){
			return $result;
		}else return false;
	}
	
	//return arguments of question
	public static function arguments($sql, $questionId) {
		//global $sql;
		$query="SELECT u.argumendid FROM ulesanne u WHERE u.idulesanne = '".$questionId."' LIMIT 1";
		$result=$sql->selectQuery($query);
		if(sizeOf($result)>0){
			return $result;
		}else return false;
	}
	
	/**
	 * Edit problem
	 */
	public static function alterProblem($sql, $questionId, $answer1,
										$answer2,$answer3,$points,
										$arguments,$description) {
		//global $sql;
		$query="UPDATE ulesanne u SET u.vastus = '".mysql_escape_string($answer1)."',
		u.vastus2 = '".mysql_escape_string($answer2)."',
		u.vastus3 = '".mysql_escape_string($answer3)."',
		 u.argumendid ='".mysql_escape_string($arguments)."',
		u.tekst ='".mysql_escape_string($description)."' 
			WHERE u.idulesanne = '".$questionId."' LIMIT 1";
		$sql->query($query);
		
		if( !($sql->affectedRows() == 1) ) {
			//check if maybe the data that is being set is already set in the db 
			$query = "SELECT * FROM ulesanne u WHERE u.vastus = '".mysql_escape_string($answer1)."' AND 
		u.vastus2 = '".mysql_escape_string($answer2)."' AND 
		u.vastus3 = '".mysql_escape_string($answer3)."' AND 
		u.argumendid ='".mysql_escape_string($arguments)."' AND 
		u.tekst ='".mysql_escape_string($description)."' AND 
		u.idulesanne = '".$questionId."' LIMIT 1";
			$sql->query($query);
			if(count($sql->selectQuery($query))==1){
					return true;
					} else return false;
		} else return true;
	}
	
	/**
	 * remove question
	 */
	public static function removeQuestion($sql, $questionId) {
		//global $sql;
		$query="DELETE FROM ulesanne
			WHERE idulesanne = '".$questionId."' LIMIT 1";

		$sql->query($query);
		if( $sql->affectedRows() == 1 ) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * remove question
	 */
	public static function getMaxPoints($sql, $questionId) {
		//global $sql;
		$query="SELECT u.maks_punkte from ulesanne u 
			WHERE u.idulesanne = '".$questionId."' LIMIT 1";

		$result = $sql->selectQuery($query);
		if( sizeOf($result)>0) {
			return $result[0]['maks_punkte'];
		}
		
		return false;
	}
	
	
}